//
//  NewsViewController.m
//  MilitaryDemo
//
//  Created by qianfeng01 on 15-7-4.
//  Copyright (c) 2015年 qianfeng01. All rights reserved.
//

#import "NewsViewController.h"
#import "NewsDetailViewController.h"
@interface NewsViewController ()

@end

@implementation NewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatTableView];
}
-(void)creatRefreshViewWithID:(int)id;
{
    __weak typeof (self) weakSelf = self;
    [weakSelf.tableView addRefreshHeaderViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        if (weakSelf.isRefreshing) {
            return ;
        }
        weakSelf.isRefreshing=YES;
        weakSelf.currentPage=1;
        [weakSelf addTaskWithUrl:[NSString stringWithFormat:KNewsUrl,id,weakSelf.currentPage-1] isRefresh:YES];
        
    }];
    [weakSelf.tableView addRefreshFooterViewWithAniViewClass:[JHRefreshCommonAniView class]  beginRefresh:^{
        if (weakSelf.isLoadMore) {
            return;
        }
        weakSelf.isLoadMore=YES;
        weakSelf.currentPage++;
        
        [weakSelf addTaskWithUrl:[NSString stringWithFormat:KNewsUrl,id,weakSelf.currentPage-1] isRefresh:YES];
        //[weakSelf addTaskWithUrl:[NSString stringWithFormat:KNewsUrl,(NSInteger)0] isRefresh:YES];
    }];
}
- (void)endRefreshing {
    if (self.isRefreshing) {
        self.isRefreshing = NO;//标记刷新结束
        //正在刷新 就结束刷新
        [self.tableView headerEndRefreshingWithResult:JHRefreshResultNone];
    }
    if (self.isLoadMore) {
        self.isLoadMore = NO;
        [self.tableView footerEndRefreshing];
    }
}
-(void)firstDownloadWithID:(int)id
{
    self.currentPage=1;
    [self addTaskWithUrl:[NSString stringWithFormat:KNewsUrl,id,self.currentPage-1] isRefresh:NO];
}
-(void)addTaskWithUrl:(NSString *)url isRefresh:(BOOL)isRefresh
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    __weak typeof (self) weakSelf=self;
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
            if (self.currentPage == 1) {
                [self.dataArr removeAllObjects];
            }
            NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            NSArray *NewsListArr=dict[@"newsList"];
            for (NSDictionary *newsDict in NewsListArr) {
                RecommedModel *model=[[RecommedModel alloc]init];
                [model setValuesForKeysWithDictionary:newsDict];
                [weakSelf.dataArr addObject:model];
            }
        }
        [weakSelf.tableView reloadData];
        [weakSelf endRefreshing];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"下载推荐数据出错");
    }];
    
}

-(void)creatTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, kScreenSize.height-64-49-30) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight=80;
    [self.view addSubview:self.tableView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{   RecommedModel *model=self.dataArr[indexPath.row];
    
    if ([model.picOne isEqual:@""]&&[model.picTwo isEqual:@""]) {
        static NSString *cellID=@"FirstCell";
        FirstCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
        if (cell==nil) {
            cell=[[[NSBundle mainBundle]loadNibNamed:@"FirstCell" owner:nil options:nil] lastObject];
        }
        cell.timeLabel.text=model.timeAgo;
        cell.titleLabel.text=model.title;
        return cell;
    }else{
        static NSString *cellID=@"SecondCell";
        SecondCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
        if (cell==nil) {
            cell=[[[NSBundle mainBundle]loadNibNamed:@"SecondCell" owner:nil options:nil] lastObject];
        }
        cell.timeLabel.text=model.timeAgo;
        cell.titleLabel.text=model.title;
        [cell.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.picOne]placeholderImage:[UIImage imageNamed:@"zhanwei"]];
        return cell;
    }
    return nil;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsDetailViewController *detailController=[[NewsDetailViewController alloc]init];
    RecommedModel *model=self.dataArr[indexPath.row];
    detailController.id=model.id;
    detailController.hidesBottomBarWhenPushed=YES;
   [self.navigationController pushViewController:detailController animated:YES];
   
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
